<?php include "../public/header.php";?>
<?php include "../public/header_public.php"?>
<?php include "../public/header_edit.php";?>
<style>
    .main-container{padding-top: 10px!important;}
    .add-tag{margin: 5px 0}
    .mainBox{bottom: 0!important;}
</style>
<body>
    <form class="layui-form flow_opt_page" id="commentForm">
        <div class="mainBox">
            <div class="title-box">
                <span class="title">进场数量</span>
                <div class="move_title"></div>
                <div class="title-box-opt">
                    <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                    <span id="page-close" class="page-close" onclick="layer_close()">×</span>
                </div>
            </div>

            <div class="main-container">
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">设备名称及规格</label>
                            <div class="layui-input-block">
                                <input type="text" id="name" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>计数单位</label>
                            <div class="layui-input-block">
                                <input type="text" id="digit" name="digit" clearable placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>进场数量</label>
                            <div class="layui-input-block">
                                <input type="number" id="jinnum" name="jinnum" clearable placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>进场日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="jindt" id="jindt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_name = getQueryVariable('name') || '';
    param_name = decodeURI(param_name);
    var param_unit = getQueryVariable('unit') || '';
    param_unit = decodeURI(param_unit);
    var param_price = getQueryVariable('price') || 0;

    $("#name").val(param_name);


    $("#commentForm").validate({
        rules: {
            digit: {required: true},
            jinnum: {required: true},
            jindt: {required: true},
        },
        messages:{
            digit: {required: "请输入"},
            jinnum: {required: "请输入"},
            jindt: {required: "请选择"},
        }
    });


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();

            data.field.jinnum = parseFloat(data.field.jinnum);
            if(data.field.jinnum < 1 || data.field.jinnum > 100){
                facade.error("进场数量,请输入1-100整数");
                validRes = false;
            }
            if(!validRes){return false;}

            data.field.listid = param_id;
            data.field.name = param_name;
            data.field.unit = param_unit;
            data.field.price = param_price;


            var str = '';
            var nums = $("#list_list tr", parent.parent.document).length;
            for (var i=1; i<=data.field.jinnum; i++){
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td>';
                str += '<input type="text" onclick="choose_contract_cai_list_one(this)" listid="'+data.field.listid+'" readonly value="'+data.field.name+'" class="layui-input name disabled">';
                str += '</td>';
                str += '<td><input type="text" readonly class="layui-input unit disabled" value="'+data.field.unit+'"></td>';
                str += '<td><input type="text" readonly class="layui-input price disabled" value="'+data.field.price+'"></td>';
                str += '<td><input type="text" class="layui-input digit" value="'+data.field.digit+'"></td>';
                str += '<td><input type="text" class="layui-input jindt bdate_ymd" value="'+data.field.jindt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input shenum"></td>';
                str += '<td><input type="text" class="layui-input explain"></td>';
                str += '</tr>';
            }
            $("#list_list", parent.parent.document).append(str);
            if (typeof(window.parent.parent.update_refresh_row_func)!="undefined"){
                window.parent.parent.update_refresh_row_func();
            }

            window.parent.layer_close();
            return false;
        });
    });








    function show_flow_used(data) {
        var str = '';
        for (var key in data){
            str += '<div class="tag-box-div">';
            str += '<span class="tag-name" onclick="pushFlowUsed(this)">'+data[key].explain+'</span>';
            str += '<span class="tag-close" onclick="delFlowUsed('+data[key].id+')">×</span>';
            str += '</div>';
        }
        $("#flow_used_div").html(str);
    }

    function addFlowUsed() {
        var explain = $('#explain').val() || '';
        if(!explain){
            facade.error('请填写处理意见');return false;
        }
        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;
            facade.ajax({url: ask_opt_add_flow_used, data:{explain:explain}}).done(function(res){
                if (res.code === 2000) {
                    show_flow_used(res.data);
                    render_from();
                }
            });
        });
    }

    function pushFlowUsed(obj){
        var value = $(obj).text();
        var explain = $('#explain').val();
        $('#explain').val(explain+value);
    }

    function delFlowUsed(id){
        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;

            facade.ajax({url: ask_opt_del_flow_used, data:{id:id}}).done(function(res){
                if (res.code === 2000) {
                    show_flow_used(res.data);
                    render_from();
                }
            });
        });
    }

</script>
</body>
</html>
